package com.hns.can.report.service;

import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.report.entity.*;
import com.hns.can.report.vo.QualityAnlsSummaryParamVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 质量分析汇总Service
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Service
public class QualityAnlsSummaryService extends CommonService {

    /**
     * 额外处理参数
     *
     * @param vo
     * @param last8Sundays
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    private void improveQualityAnlsSummaryParamVo(QualityAnlsSummaryParamVo vo,boolean last8Sundays)throws Exception{
        if(PubUtil.isNotEmpty(vo.getReportDate())){
            //根据日期计算所在周的周一和周日
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
            Calendar cal = Calendar.getInstance();
            Date time=sdf.parse(vo.getReportDate());
            cal.setTime(time);
            //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
            int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
            if(1 == dayWeek) {
                cal.add(Calendar.DAY_OF_MONTH, -1);
            }
//        System.out.println("要计算日期为:"+sdf.format(cal.getTime())); //输出要计算日期
            cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
            int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
            cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
            String beginTime = sdf.format(cal.getTime());
            vo.setBeginTime(beginTime);
//        System.out.println("所在周星期一的日期："+beginTime);
            cal.add(Calendar.DATE, 6);
            String endTime = sdf.format(cal.getTime());
            vo.setEndTime(endTime);
//        System.out.println("所在周星期日的日期："+endTime);
            if(last8Sundays){
                //根据日期计算最近的8个周日
                String[] sundays = new String[8];
                sundays[0]=endTime;
                for(int i=1;i<8;i++){
                    cal.add(Calendar.DATE, -7);
                    endTime = sdf.format(cal.getTime());
                    sundays[i]=endTime;
                }
                vo.setReportDates(sundays);
            }
        }

    }

    /**
     * 分页查询车辆行为统计周报
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    @LeftOrgAnnotation()
    public Pagination searchTBsStatWeakBhvPage(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,false);
        Pagination page = this.myBatisPage("qualityAnlsSummary.getTBsStatWeakBhvCnt","qualityAnlsSummary.getTBsStatWeakBhvList", vo, TBsStatWeakBhvEntity.class);
        return page;
    }

    /**
     * 查询最近8周车辆行为统计周报
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    public List<TBsStatWeakBhvEntity> getLast8TBsStatWeakBhvList(QualityAnlsSummaryParamVo vo)throws Exception {
        improveQualityAnlsSummaryParamVo(vo,true);
        List<TBsStatWeakBhvEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("qualityAnlsSummary.getLast8TBsStatWeakBhvList", vo), TBsStatWeakBhvEntity.class);
        return list;
    }

    /**
     * 查询车辆行为统计周报用于导出
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    @LeftOrgAnnotation()
    public List<TBsStatWeakBhvEntity> searchTBsStatWeakBhvList(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,true);
        List<TBsStatWeakBhvEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("qualityAnlsSummary.getTBsStatWeakBhvList", vo), TBsStatWeakBhvEntity.class);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }

    /**
     * 分页查询车辆能耗统计周报
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    @LeftOrgAnnotation()
    public Pagination searchTBsStatWeakEnerConsPage(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,false);
        Pagination page = this.myBatisPage("qualityAnlsSummary.getTBsStatWeakEnerConsCnt","qualityAnlsSummary.getTBsStatWeakEnerConsList", vo, TBsStatWeakEnerConsEntity.class);
        return page;
    }

    /**
     * 查询车辆能耗统计周报用于导出
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    @LeftOrgAnnotation()
    public List<TBsStatWeakEnerConsEntity> searchTBsStatWeakEnerConsList(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,true);
        List<TBsStatWeakEnerConsEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("qualityAnlsSummary.getTBsStatWeakEnerConsList", vo), TBsStatWeakEnerConsEntity.class);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }

    /**
     * 分页查询车辆故障统计周报
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    @LeftOrgAnnotation()
    public Pagination searchTBsStatWeakMftPage(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,false);
        Pagination page = this.myBatisPage("qualityAnlsSummary.getTBsStatWeakMftCnt","qualityAnlsSummary.getTBsStatWeakMftList", vo, TBsStatWeakMftEntity.class);
        return page;
    }

    /**
     * 查询最近8周车辆故障统计周报
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    public List<TBsStatWeakMftEntity> getLast8TBsStatWeakMftList(QualityAnlsSummaryParamVo vo)throws Exception {
        improveQualityAnlsSummaryParamVo(vo,true);
        List<TBsStatWeakMftEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("qualityAnlsSummary.getLast8TBsStatWeakMftList", vo), TBsStatWeakMftEntity.class);
        return list;
    }

    /**
     * 查询车辆故障统计周报用于导出
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    @LeftOrgAnnotation()
    public List<TBsStatWeakMftEntity> searchTBsStatWeakMftList(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,true);
        List<TBsStatWeakMftEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("qualityAnlsSummary.getTBsStatWeakMftList", vo), TBsStatWeakMftEntity.class);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }

    /**
     * 分页查询车辆里程统计周报
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    @LeftOrgAnnotation()
    public Pagination searchTBsStatWeakMilePage(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,false);
        Pagination page = this.myBatisPage("qualityAnlsSummary.getTBsStatWeakMileCnt","qualityAnlsSummary.getTBsStatWeakMileList", vo, TBsStatWeakMileEntity.class);
        return page;
    }

    /**
     * 查询车辆里程统计周报用于导出
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    @LeftOrgAnnotation()
    public List<TBsStatWeakMileEntity> searchTBsStatWeakMileList(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,true);
        List<TBsStatWeakMileEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("qualityAnlsSummary.getTBsStatWeakMileList", vo), TBsStatWeakMileEntity.class);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }

    /**
     * 分页查询车辆在线率统计周报
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    @LeftOrgAnnotation()
    public Pagination searchTBsStatWeakOnlinePage(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,false);
        Pagination page = this.myBatisPage("qualityAnlsSummary.getTBsStatWeakOnlineCnt","qualityAnlsSummary.getTBsStatWeakOnlineList", vo, TBsStatWeakOnlineEntity.class);
        return page;
    }

    /**
     * 查询最近8周车辆在线率统计周报
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    public List<TBsStatWeakOnlineEntity> getLast8TBsStatWeakOnlineList(QualityAnlsSummaryParamVo vo)throws Exception {
        improveQualityAnlsSummaryParamVo(vo,true);
        List<TBsStatWeakOnlineEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("qualityAnlsSummary.getLast8TBsStatWeakOnlineList", vo), TBsStatWeakOnlineEntity.class);
        return list;
    }

    /**
     * 查询车辆在线率统计周报用于导出
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @Transactional
    @LeftOrgAnnotation()
    public List<TBsStatWeakOnlineEntity> searchTBsStatWeakOnlineList(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,true);
        List<TBsStatWeakOnlineEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("qualityAnlsSummary.getTBsStatWeakOnlineList", vo), TBsStatWeakOnlineEntity.class);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }

    /**
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:chenl
     * @email:370238764@qq.com
     */
    @Transactional
    @LeftOrgAnnotation()
    public Pagination searchTBsStatWeakStopPage(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,false);
        Pagination page = this.myBatisPage("qualityAnlsSummary.searchTBsStatWeakStopCnt","qualityAnlsSummary.searchTBsStatWeakStopList", vo, TBsStatWeakStnEntity.class);
        return page;
    }

    /**
     * 查询车辆报站统计周报用于导出
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:chenl
     * @email:
     */
    @Transactional
    @LeftOrgAnnotation()
    public List<TBsStatWeakStnEntity> searchTBsStatWeakStnList(QualityAnlsSummaryParamVo vo) throws Exception {
        improveQualityAnlsSummaryParamVo(vo,true);
        List<TBsStatWeakStnEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("qualityAnlsSummary.searchTBsStatWeakStopList", vo), TBsStatWeakStnEntity.class);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }
}
